package com.qst.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.Service.AdminService;
import com.qst.Vo.AdminVo;
import com.qst.dao.AdminMapper;
import com.qst.pojo.Admin;
import com.qst.pojo.Room;
import jdk.nashorn.internal.runtime.JSONFunctions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * @packageName:com.qst.controller
 * @className:AdminController
 * @Description:
 * @author:zhangyu
 * @date 2021/6/20 22:26
 **/

@Controller
public class AdminController {

    @Resource
    AdminService adminService;

    @RequestMapping(value = "/adminfindbyloginname")
    public ModelAndView adminfindbyloginname(HttpSession session, String loginname) {
        String selectloginname = (String) session.getAttribute("selectfromloginname");
        List<Admin> adminList = adminService.findAdminbyname(selectloginname);
        System.out.println(adminList);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("adminAll", adminList);
        modelAndView.setViewName("/admin/adminme");
        return modelAndView;
    }

    @RequestMapping(value = "/adminupdate")
    public String adminupdate(Admin admin) {
        adminService.updateAdmin(admin);
        return "redirect:adminfindbyloginname.do";
    }

    @RequestMapping(value = "/adminfindbyid")
    public ModelAndView adminfindbyid(int id) {
        List<Admin> adminList = adminService.findAdminbyid(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("adminid", adminList);
        modelAndView.setViewName("/admin/updateadmin");
        return modelAndView;
    }

    @RequestMapping(value = "/findalladmin")
    @ResponseBody
    public Map<String, Object> findalladmin(AdminVo adminVo) {

        PageHelper.startPage(adminVo.getPage(),adminVo.getLimit());
        List<Admin> adminList = adminService.findAllAdmin(adminVo);

        Map<String, Object> map = new HashMap<String, Object>();
        PageInfo<Admin> pageInfo = new PageInfo<Admin>(adminList);
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        return map;
    }

    @RequestMapping(value= "/addadmin")
    @ResponseBody
    public Map<String,Object> addadmin(Admin admin){
        Map<String,Object> map = new HashMap<String, Object>();
        if(adminService.addadmin(admin)>0){
            map.put("success",true);
            map.put("message","添加成功");
        }else{
            map.put("success",false);
            map.put("message","添加失败");
        }
        return map;


    }

    @RequestMapping(value = "/updateadmin")
    @ResponseBody
    public Map<String,Object> updateadmin(Admin admin){
        Map<String,Object> map = new HashMap<String, Object>();
        if(adminService.updateAdmin(admin)>0){
            map.put("success",true);
            map.put("message","修改成功");
        }else{
            map.put("success",false);
            map.put("message","修改失败");
        }
        return map;
}

    @RequestMapping(value = "/deleteadmin")
    @ResponseBody
    public Map<String,Object> deleteadmin(int id) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (adminService.deleteadmin(id) > 0) {
            map.put("success", true);
            map.put("message", "删除成功");
        } else {
            map.put("success", false);
            map.put("message", "删除失败");
        }
        return map;
    }

}
